package theOrbalEra.common.block;

import theOrbalEra.client.gui.GuiQuatzExchange;
import theOrbalEra.common.network.PacketGetQuartz;
import theOrbalEra.common.util.DefultValue;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class OrbalBlockQuatzExchange extends OrbalBlock {
	
	 private Icon front;
	  private Icon down;
	  private Icon up;
	  
	public OrbalBlockQuatzExchange(int par1) {
		super(par1, Material.iron);
	}
	 
	
	
	
	public boolean onBlockActivated(World par1World, int par2, int par3,int par4, EntityPlayer par5EntityPlayer, int par6, float par7,
			float par8, float par9) {
		if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) {
			FMLClientHandler.instance().showGuiScreen(new GuiQuatzExchange(this, par5EntityPlayer));
		}
		return true;
	}
	
	  public void getLoopPayBag(int loopId,  int[] ele, int[] need) {
		
		PacketGetQuartz.getQuatz(loopId, ele,need);
		
	}
	
	  @Override
		 public Icon getIcon(int par1, int par2)
		    {
		        if(par1==1)
		        	return this.up;
		        else if(par1==0)
		        	return this.down;
		        else if(par1==3)
		        	return this.front;
		        else
		        	return this.blockIcon;
		    }
		 
	    
	    @Override
		public Icon getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
	    {
	        if (par5 == 1)
	        {
	            return this.up;
	        }
	        else if(par5 == 0){
			return this.down;
			}
			else
	        {
	            int var6 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
	            return par5 != var6 ? this.blockIcon : this.front;
	        }
	    }
	    @Override
		public void onBlockAdded(World par1World, int par2, int par3, int par4)
	    {
	        super.onBlockAdded(par1World, par2, par3, par4);
	        this.setDefaultDirection(par1World, par2, par3, par4);
	    }
		 private void setDefaultDirection(World par1World, int par2, int par3, int par4)
		    {
		        if (!par1World.isRemote)
		        {
		            int var5 = par1World.getBlockId(par2, par3, par4 - 1);
		            int var6 = par1World.getBlockId(par2, par3, par4 + 1);
		            int var7 = par1World.getBlockId(par2 - 1, par3, par4);
		            int var8 = par1World.getBlockId(par2 + 1, par3, par4);
		            byte var9 = 3;

		            if (Block.opaqueCubeLookup[var5] && !Block.opaqueCubeLookup[var6])
		            {
		                var9 = 3;
		            }

		            if (Block.opaqueCubeLookup[var6] && !Block.opaqueCubeLookup[var5])
		            {
		                var9 = 2;
		            }

		            if (Block.opaqueCubeLookup[var7] && !Block.opaqueCubeLookup[var8])
		            {
		                var9 = 5;
		            }

		            if (Block.opaqueCubeLookup[var8] && !Block.opaqueCubeLookup[var7])
		            {
		                var9 = 4;
		            }

		            par1World.setBlockMetadataWithNotify(par2, par3, par4, var9,2);
		        }
		    }
		 public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)
		    {
		        int var6 = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

		        if (var6 == 0)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2,2);
		        }

		        if (var6 == 1)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 5,2);
		        }

		        if (var6 == 2)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3,2);
		        }

		        if (var6 == 3)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 4,2);
		        }
		    }
		
	    
	  
		 @Override
			@SideOnly(Side.CLIENT)
			public void registerIcons(IconRegister par1IconRegister) {
				this.front=par1IconRegister.registerIcon(DefultValue.ModName+":QuatzExchangeF");
				this.blockIcon=par1IconRegister.registerIcon(DefultValue.ModName+":QuatzExchangeN");
				this.up=par1IconRegister.registerIcon(DefultValue.ModName+":QuatzExchangeU");
				this.down=par1IconRegister.registerIcon(DefultValue.ModName+":QuatzExchangeD");
			}
			
		

	
	
	

}
